//
//  HRCurveLineView.m
//  TemperatureLine
//
//  Created by Chen Heren on 16/8/25.
//  Copyright (c) 2016年 Chen Heren. All rights reserved.
//

#import "HRCurveLineView.h"

static NSInteger lead = 20;  //头部
static NSInteger trail= 20;  //尾部

@interface HRCurveLineView()

@property (nonatomic, copy) NSArray *XArray;
@property (nonatomic, copy) NSArray *YArray;


@end

@implementation HRCurveLineView
{
    NSUInteger countX;
    NSUInteger countY;
    NSUInteger unitX;
    CGFloat max;
    CGFloat min;
    
    NSMutableArray *points;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        self.isShowLine = NO;
        self.isShowZeroLine = YES;
        self.edgeInset = UIEdgeInsetsMake(10, 10, 30, 10);
        self.dotType = DotTypeCircular;
    }
    return self;
}

-(void)calculatorWithXArray:(NSArray *)XArray andYArray:(NSArray *)YArray{
    
    self.XArray = XArray;
    self.YArray = YArray;
    
    countX = self.XArray.count;
    countY = self.YArray.count;
    
    min = [self.XArray[0] floatValue];
    max = [self.XArray[0] floatValue];
    for (NSNumber *number in self.YArray) {
        CGFloat y = [number floatValue];
        if (y > max) {
            max = y;
        }
        if (y < min) {
            min = y;
        }
        
    }
    
    unitX = (self.frame.size.width - lead - trail - self.edgeInset.left - self.edgeInset.right)/(countX-1);
    points = [[NSMutableArray alloc]init];
    for (int i=0; i<countX; i++) {
        CGFloat x = self.edgeInset.left + lead + unitX *i;
        CGFloat y = self.frame.size.height - self.edgeInset.bottom- ([self.YArray[i] floatValue] - min) * (self.frame.size.height - self.edgeInset.top - self.edgeInset.bottom) /(max - min);
        CGPoint point = CGPointMake(x,y);
        NSValue *value  = [NSValue valueWithBytes:&point objCType:@encode(CGPoint)];
        [points addObject:value];
        
    }
    
}

-(void)drawXaxis{
    // x轴
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(self.edgeInset.left, self.frame.size.height- self.edgeInset.bottom +10)];
    [path addLineToPoint:CGPointMake(self.frame.size.width- self.edgeInset.right, self.frame.size.height- self.edgeInset.bottom+10)];
    
    //标注刻度
    for (NSInteger i = 0; i<=countX; i++) {
        [path moveToPoint:CGPointMake(self.edgeInset.left + unitX * i+ lead, self.frame.size.height- self.edgeInset.bottom+10)];
        [path addLineToPoint:CGPointMake(self.edgeInset.left + unitX *i +lead, self.frame.size.height- self.edgeInset.bottom +5)];
        
    }
    
    CAShapeLayer *layer = [[CAShapeLayer alloc]init];
    layer.path = path.CGPath;
    layer.strokeColor = [UIColor brownColor].CGColor;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.lineWidth = 2.0;
    [self.layer addSublayer:layer];
    

    
    //添加标签
    for (NSInteger i = 0; i<countX; i++) {
        UILabel *label = [[UILabel alloc]init];
        CGRect frame = label.frame;
        frame.size = CGSizeMake(30, 20);
        label.frame = frame;
        label.center = CGPointMake(self.edgeInset.left + unitX * i+ lead, self.frame.size.height- self.edgeInset.bottom+21);
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:12];
        label.text = self.XArray[i];
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
    }
    
    
}

//画点
-(void)drawPoint{
    switch (self.dotType) {
        case DotTypeSquare:
            for (int i = 0; i <self.YArray.count; i++) {
                CGPoint point;
                [((NSValue *)points[i]) getValue:&point];   //数组取值
                CAShapeLayer *layer = [[CAShapeLayer alloc]init];
                layer.frame = CGRectMake(point.x-2, point.y-2, 4, 4);
                layer.backgroundColor = [UIColor redColor].CGColor;
                [self.layer  addSublayer:layer];
            }
            break;
         case DotTypeCircular:
            for (int i= 0; i<self.YArray.count; i++) {
                CGPoint point;
                [((NSValue *)points[i]) getValue:&point];   //数组取值
                UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(point.x-2, point.y-2, 4, 4) cornerRadius:2.5];
                CAShapeLayer *layer = [CAShapeLayer layer];
                layer.path = path.CGPath;
                layer.strokeColor = [UIColor redColor].CGColor;
                layer.fillColor = [UIColor redColor].CGColor;
                [self.layer addSublayer:layer];
            }
        default:
            break;
    }
}

//划线
-(void)drawLine{
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint point;
    [((NSValue *)points[0]) getValue:&point];   //数组取值
    [path moveToPoint:point];
    for (int i = 1; i<countX; i++) {
        [((NSValue *)points[i]) getValue:&point];   //数组取值
        [path addLineToPoint:point];
    }
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.strokeColor = [UIColor redColor].CGColor;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.lineWidth = 1;
    [self.layer addSublayer:layer];
}

//添加数据标签
-(void)addDataLabel{
    //添加标签
    for (NSInteger i = 0; i<countX; i++) {
        UILabel *label = [[UILabel alloc]init];
        CGRect frame = label.frame;
        frame.size = CGSizeMake(30, 20);
        label.frame = frame;
        CGPoint point;
        [((NSValue *)points[i]) getValue:&point];   //数组取值
        label.center = CGPointMake(point.x, point.y - 11);
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:10];
        label.text = self.YArray[i];
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
    }
}

//添加0线
-(void)addZeroLine{
    if (min<0 && self.isShowZeroLine) {
        UIBezierPath *path = [UIBezierPath bezierPath];
        CGFloat y = self.frame.size.height - (0-min)*(self.frame.size.height - self.edgeInset.top - self.edgeInset.bottom)/(max-min)-self.edgeInset.bottom;
        [path moveToPoint:CGPointMake(self.edgeInset.left+10, y)];
        [path addLineToPoint:CGPointMake(self.frame.size.width - self.edgeInset.right-10, y)];
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.path = path.CGPath;
        layer.lineWidth = 0.5;
        layer.strokeColor = [UIColor purpleColor].CGColor;
        [self.layer addSublayer:layer];
        
        UILabel *label = [[UILabel alloc]init];
        CGRect frame = label.frame;
        frame.size = CGSizeMake(30, 20);
        label.frame = frame;
        label.center = CGPointMake(self.frame.size.width - self.edgeInset.right-3, y);
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:10];
        label.text = @"0°";
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        
    }
}

-(void)setuWithXArray:(NSArray *)XArray andYArray:(NSArray *)YArray{
    [self calculatorWithXArray:XArray andYArray:YArray];
    [self drawXaxis];
    [self drawPoint];
    [self drawLine];
    [self addDataLabel];
    [self addZeroLine];
}

@end
